package com.wnhz.work.jy.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class PayPasswordActivity extends BaseActivity implements OnClickListener {
    private LinearLayout backManage;
    private TextView tv_payPassword_next;
    private int time = 0;
    private TextView payPassword_bound_phone;//手机号码
    private TextView get_verification_code_tv;// 获取验证码
    private EditText verification_code;// 验证码

    private String phoneNumber;

    private TextView title;
    private EditText et_phone;
    private String type;

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 31:
                    time--;
                    if (time == 0) {
                        get_verification_code_tv.setText("获取验证码");
                        setba();
                        return;
                    }

                    get_verification_code_tv.setText("重新获取" + time);
                    get_verification_code_tv.setSelected(false);
                    handler.sendEmptyMessageDelayed(31, 1000);

                    break;

                default:
                    break;
            }
        }

        ;
    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_paypassword);
        type = getIntent().getStringExtra("type");
        initView();
        setba();
        initTirle();
        payPassword_bound_phone.setText(MyApplication.getInstance().userBean.getPhoneNumber());

        /*if ("手机号".equals(type)){
            et_phone.setVisibility(View.VISIBLE);
            payPassword_bound_phone.setVisibility(View.GONE);
        }else {
            et_phone.setVisibility(View.GONE);
            payPassword_bound_phone.setVisibility(View.VISIBLE);
        }*/
    }

    private void initTirle() {
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("验证手机");


    }

    private void initView() {
        et_phone = (EditText) findViewById(R.id.et_phone);
        get_verification_code_tv = (TextView) findViewById(R.id.get_verification_code);
        payPassword_bound_phone = (TextView) findViewById(R.id.payPassword_bound_phone);
        verification_code = (EditText) findViewById(R.id.verification_code);
        findViewById(R.id.tv_payPassword_next).setOnClickListener(this);

        get_verification_code_tv.setOnClickListener(this);
    }


    private void setba() {
        get_verification_code_tv.setSelected(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //获取验证码
            case R.id.get_verification_code:
                /*if (time == 0) {
                    time = 60;

                    handler.sendEmptyMessage(31);
                }*/

                getCode();
                break;

            //下一步
            case R.id.tv_payPassword_next:
                if ("".equals(verification_code.getText().toString())) {
                    Toast.makeText(PayPasswordActivity.this, "验证码不能为空", Toast.LENGTH_SHORT).show();
                } else if (verification_code.getText().toString().equals(first_code)) {
                    if ("更换密码".equals(type)) {
                        startActivity(new Intent(PayPasswordActivity.this, ChangeActivity.class).putExtra("type", "更换登录密码"));
                    } else if ("手机号".equals(type)) {
                        startActivity(new Intent(PayPasswordActivity.this, ChangeActivity.class).putExtra("type", "更换手机号"));
                    } else {
                        startActivity(new Intent(PayPasswordActivity.this, InputPasswordActivity.class));
                    }
                    finish();

                } else {
                    Toast.makeText(PayPasswordActivity.this, "您输入的验证码有误，请重新输入", Toast.LENGTH_SHORT).show();
                }
                break;


            case R.id.btn_main_left:
                finish();
                break;
            default:
                break;
        }

    }

    private String oldphone;
    private String codetime;
    private String first_code;
    private static final String TAG = "PayPasswordActivity";

    private void getCode() {
        /*if ("更换密码".equals(type)){
            phoneNumber = et_phone.getText().toString();
        }else {
            phoneNumber = payPassword_bound_phone.getText().toString();
        }*/
        phoneNumber = payPassword_bound_phone.getText().toString();

        Map<String, Object> map = new HashMap<>();
        map.put("phoneNumber", phoneNumber);
        showDialog();
        XUtil.Post(ConfigUtil.GETPAYYZM, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: code  " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("result");
                    if ("1".equals(re)) {
                        JSONObject jsonObject1 = jsonObject.optJSONObject("info");
                        oldphone = jsonObject1.optString("phone");
                        codetime = jsonObject1.optString("time");
                        first_code = jsonObject1.optString("code");
                        time = 60;
                        handler.sendEmptyMessage(31);
                    } else {
                        String info = jsonObject.optString("info");
                        Toast.makeText(PayPasswordActivity.this, info, Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
                get_verification_code_tv.setFocusable(true);
            }
        });
    }

}